//
//  PublicServerView.m
//  hnGovernment
//
//  Created by 安仔 on 17/2/25.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "PublicServerView.h"
#import "PublicServerCell.h"
#import "SelectServiceObj.h"
#import "UIImageView+WebCache.h"
@interface PublicServerView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectView;
//数据源
@property(nonatomic, strong) NSArray *collectDataArray;

@end

@implementation PublicServerView

- (instancetype)initWithFrame:(CGRect)frame withData:(NSArray *)dataArr{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.collectDataArray = dataArr;
        [self initCollectionView];
    }
    return self;
}

//创建承载微应用的collectionView
- (void)initCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, self.bounds.size.height) collectionViewLayout:layout];
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    self.collectView.showsVerticalScrollIndicator = NO;
    self.collectView.showsHorizontalScrollIndicator = NO;
    self.collectView.backgroundColor = [UIColor whiteColor];
    
    //注册 colllectionCell
    [self.collectView registerClass:[PublicServerCell class] forCellWithReuseIdentifier:@"PublicServerCell"];
    [self addSubview:self.collectView];

}

#pragma mark- UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.collectDataArray.count == 0) {
        return 0;
    }
    else {
        return self.collectDataArray.count;
    }
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PublicServerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PublicServerCell" forIndexPath:indexPath];
    SelectServiceObj *obj = self.collectDataArray[indexPath.row];
    cell.titleLab.text = obj.serviceName;
    [cell.signImg sd_setImageWithURL:[NSURL URLWithString:obj.serviceImg]];
   
    return cell;
}

#pragma mark- collectionView delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];

    if ([self.delegate respondsToSelector:@selector(selectPublicServerItem:)]) {
        [self.delegate selectPublicServerItem:indexPath.row];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(KScreenWidth / 4, self.collectView.bounds.size.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
}


//刷新网格数据
- (void) setData:(NSArray *)dataArr {
    self.collectDataArray = dataArr;
    [self.collectView reloadData];
}

@end
